﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThouShaltNotBrick.Models;
using System.Xml.Linq;
using System.IO;
using GMLogging;

namespace ThouShaltNotBrick.DataAccess
{
    public static class LevelXmlReader
    {
        #region Methods
        public static LevelData Read(string pLevelFilePath)
        {
            LevelData levelData = null;
            FileStream stream = null;
            try
            {
                stream = new FileStream(pLevelFilePath, FileMode.Open, FileAccess.Read);
                XDocument levelXDocument = XDocument.Load(stream);
                levelData = Parse(levelXDocument);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            return levelData;
        }

        public static LevelData Parse(string pLevelInputString)
        {
            try
            {
                XDocument levelXDocument = XDocument.Parse(pLevelInputString);
                return Parse(levelXDocument);
            }
            catch (Exception e)
            {
                Logger.Error(e.Message);
                throw;
            }
        }

        private static LevelData Parse(XDocument pLevelXDocument)
        {
            XElement levelXElement = pLevelXDocument.Element(LevelXmlConstants.TAG_LEVEL);
            XAttribute levelNameXAttribute = levelXElement.Attribute(LevelXmlConstants.TAG_LEVEL_ATTRIBUTE_NAME);
            string levelName = levelNameXAttribute.Value;

            XElement bricksXElement = levelXElement.Element(LevelXmlConstants.TAG_BRICKS);

            List<XElement> brickXElements = bricksXElement.Elements(LevelXmlConstants.TAG_BRICK).ToList();
            List<BrickData> brickList = new List<BrickData>();
            foreach (XElement brickXElement in brickXElements)
            {
                XAttribute typeXAttribute = brickXElement.Attribute(LevelXmlConstants.TAG_BRICK_ATTRIBUTE_TYPE);
                XAttribute rowXAttribute = brickXElement.Attribute(LevelXmlConstants.TAG_BRICK_ATTRIBUTE_ROW);
                XAttribute columnXAttribute = brickXElement.Attribute(LevelXmlConstants.TAG_BRICK_ATTRIBUTE_COLUMN);
                XAttribute initialStateXAttribute = brickXElement.Attribute(LevelXmlConstants.TAG_BRICK_ATTRIBUTE_INITIAL_STATE);

                string type = typeXAttribute.Value;
                int row = int.Parse(rowXAttribute.Value);
                int column = int.Parse(columnXAttribute.Value);
                int initialState = int.Parse(initialStateXAttribute.Value);

                BrickData brickData = new BrickData(type, row, column, initialState);
                brickList.Add(brickData);
            }

            LevelData level = new LevelData(levelName, brickList);
            return level;
        }
        #endregion
    }
}